#!/bin/bash
# 一键部署所有服务

set -e

echo "开始一键部署所有服务..."

# 检查是否在项目根目录
if [ ! -d "scripts/deploy" ]; then
    echo "错误: 请在项目根目录执行此脚本"
    exit 1
fi

# 获取脚本目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "=========================================="
echo "部署顺序:"
echo "1. API网关服务 (端口1000)"
echo "2. 微信服务 (端口1001)"
echo "3. GPU自动扩缩容系统 (端口2000)"
echo "=========================================="

# 1. 部署API网关
echo ""
echo "🚀 步骤1: 部署API网关服务..."
bash "$SCRIPT_DIR/deploy-gateway.sh"

# 2. 部署微信服务
echo ""
echo "🚀 步骤2: 部署微信服务..."
bash "$SCRIPT_DIR/deploy-wechat.sh"

# 3. 部署自动扩缩容系统
echo ""
echo "🚀 步骤3: 部署GPU自动扩缩容系统..."
bash "$SCRIPT_DIR/deploy-autoscaling.sh"

# 检查所有服务状态
echo ""
echo "=========================================="
echo "🔍 检查所有服务状态..."
echo "=========================================="

# 检查API网关
if curl -s http://127.0.0.1:1000/api/v1/health > /dev/null; then
    echo "✅ API网关服务 (端口1000) - 正常"
else
    echo "❌ API网关服务 (端口1000) - 异常"
fi

# 检查微信服务
if curl -s http://127.0.0.1:1001/api/wechat/health > /dev/null; then
    echo "✅ 微信服务 (端口1001) - 正常"
else
    echo "❌ 微信服务 (端口1001) - 异常"
fi

# 检查自动扩缩容系统
if curl -s http://127.0.0.1:2000/health > /dev/null; then
    echo "✅ GPU自动扩缩容系统 (端口2000) - 正常"
else
    echo "❌ GPU自动扩缩容系统 (端口2000) - 异常"
fi

echo ""
echo "=========================================="
echo "🎉 部署完成！"
echo "=========================================="
echo ""
echo "服务访问地址:"
echo "- API网关: http://127.0.0.1:1000"
echo "- 微信服务: http://127.0.0.1:1001"
echo "- 自动扩缩容: http://127.0.0.1:2000"
echo ""
echo "日志位置:"
echo "- 网关日志: 1-gateway/shared/logs/"
echo "- 自动扩缩容日志: 4-management/01-gpu-autoscaling/logs/"
echo ""
echo "下一步:"
echo "1. 更新Nginx配置指向新端口"
echo "2. 重载Nginx: sudo nginx -s reload"
echo "3. 测试外部访问"
echo "=========================================="
